﻿
using System.Linq;
namespace SanguoSharp.Server.Cards
{
    public sealed class 乐不思蜀 : TableCard
    {
        private sealed class HappinessResponseHandler : ResponseHandler
        {
            private 乐不思蜀 parent;

            public HappinessResponseHandler(乐不思蜀 parent)
                : base(RequestType.HappinessTarget, parent.Owner)
            {
                this.parent = parent;
            }

            protected override void OnRespondPlayer(Player player)
            {
                //不能给死人用
                if (!player.IsAlive)
                    throw new BadTargetPlayerException();;
                //不能给已经乐的人用
                if (player.Table.Any(c => c.Type == CardType.乐不思蜀))
                    throw new BadTargetPlayerException();

                Game.GameTable.PassTableCard(parent, player);
                End();
            }

            protected override void OnRespondNoAction()
            {
                End();
            }
        }
        public 乐不思蜀(Game game, int id, CardSuit suit, CardRank rank)
            : base(game, id, CardType.乐不思蜀, suit, rank)
        {
        }

        protected override void OnPlay()
        {
            if (!IsInHand)
                throw new BadUsageException();
            Game.GameCycle.PushTempHandler(new HappinessResponseHandler(this));
        }

        protected override void OnPredrawCheck()
        {
            Owner.CheckDeck(this, c => c.Suit == CardSuit.红桃, OnResult);
        }

        private void OnResult(Card causedBy, bool result)
        {
            Player owner = Owner;
            Game.GameTable.CancelCard(this);
            if (!result)
                owner.SkipTurn = true;
        }
    }
}

